import com.lagou.dao.IUserDao;
import com.lagou.pojo.User;
import com.lagou.io.Resource;
import com.lagou.sqlsession.SqlSession;
import com.lagou.sqlsession.SqlSessionFactory;
import com.lagou.sqlsession.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.InputStream;
import java.sql.*;
import java.util.List;

/**
 * 自定义持久层框架测试类
 */
public class OrmTest {

    @Test
    public void testSelectList() throws Exception {
        InputStream in = Resource.getResourceAsSteam("sqlMapConfig.xml");
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sessionFactory = builder.build(in);
        SqlSession sqlSession = sessionFactory.openSqlSession();
//        List<User> users = sqlSession.selectList("UserMapper.selectList");
        IUserDao userDao = sqlSession.getMapper(IUserDao.class);
        List<User> users = userDao.findAllUser();
        for (User user:users){
            System.out.println(user.toString());
        }


    }

    @Test
    public void testSelectOne() throws Exception {
        InputStream in = Resource.getResourceAsSteam("sqlMapConfig.xml");
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sessionFactory = builder.build(in);
        SqlSession sqlSession = sessionFactory.openSqlSession();
        User param = new User();
        param.setId(2);
        param.setName("李四");
        //优化点 statementId 硬编码
//        User user = sqlSession.selectOne("UserMapper.selectOne",param);
        IUserDao userDao = sqlSession.getMapper(IUserDao.class);
        User user = userDao.findUserByParam(param);
        System.out.println(user.toString());
    }

    @Test
    public void testJdbc(){
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            //1.加载数据库驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2.获取数据库连接
            connection = DriverManager.getConnection("jdbc:mysql:///mybatis",
                    "root","root");
            //3.准备SQL语句
            String sql = "select * from user where id = ? and username = ?";
            //4.获取PrepareStatement对象
            preparedStatement = connection.prepareStatement(sql);
            //5.设置参数
            preparedStatement.setObject(1,"1");
            preparedStatement.setObject(2,"张三");
            //6.执行SQL，返回结果集ResultSet
            resultSet = preparedStatement.executeQuery();
            //7.遍历ResultSet,获取对应字段值，封装返回对象
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String username = resultSet.getString("username");
                User user = new User();
                user.setId(id);
                user.setName(username);
                System.out.println(user.toString());
            }
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            //8.关闭资源
            if(resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(preparedStatement != null) {
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                ;
            }
        }

    }
    @Test
    public void testInsert() throws Exception {
        InputStream in = Resource.getResourceAsSteam("sqlMapConfig.xml");
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sessionFactory = builder.build(in);
        SqlSession sqlSession = sessionFactory.openSqlSession();
        User param = new User();
        param.setId(6);
        param.setName("李四2");
        IUserDao userDao = sqlSession.getMapper(IUserDao.class);
        int count = userDao.insertUser(param);
        System.out.println("count:"+count);
    }

    @Test
    public void testUpdate() throws Exception {
        InputStream in = Resource.getResourceAsSteam("sqlMapConfig.xml");
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sessionFactory = builder.build(in);
        SqlSession sqlSession = sessionFactory.openSqlSession();
        User param = new User();
        param.setId(6);
        param.setName("李四更新");
        IUserDao userDao = sqlSession.getMapper(IUserDao.class);
        int count = userDao.updateUser(param);
        System.out.println("count:"+count);
    }

    @Test
    public void testDelete() throws Exception {
        InputStream in = Resource.getResourceAsSteam("sqlMapConfig.xml");
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sessionFactory = builder.build(in);
        SqlSession sqlSession = sessionFactory.openSqlSession();
        Integer id = 3;
        IUserDao userDao = sqlSession.getMapper(IUserDao.class);
        int count = userDao.deleteUser(3);
        System.out.println("count:"+count);
    }
}
